package com.tsl.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsl.reggie.common.R;
import com.tsl.reggie.domain.dto.SetmealDto;
import com.tsl.reggie.domain.po.Category;
import com.tsl.reggie.domain.po.Setmeal;
import com.tsl.reggie.service.ICategoryService;
import com.tsl.reggie.service.ISetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author 唐书亮
 * @since 2024-01-20
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;
    @Autowired
    private ICategoryService categoryService;
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> saveSetmealDish(@RequestBody SetmealDto setmealDto) {


        return setmealService.saveSetmealDish(setmealDto);
    }
    @Cacheable(value = "setmealCache",key = "#pageSize+'_'+#page" ,unless = "result == null")
    @GetMapping("page")
    public R<Page<SetmealDto>> selectPageSetmeal(Integer pageSize, Integer page, String name) {
        //构造分页查询条件
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        //构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Setmeal::getName,name);
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);
        //执行分页查询
        pageInfo = setmealService.page(pageInfo, queryWrapper);
        //处理setmeal和setmealDto的值,把前者赋值给后,records单独处理
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        //处理Page里的records
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> setmealDtos = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            //拿到categoryId,然后查询出category的数据
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            //拿到categoryName赋值给setmealDto里的categoryName
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(setmealDtos);
        //返回结果
        return R.success(dtoPage);
    }
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status" )
    @GetMapping("list")
    public R<List<Setmeal>> listSetmeal(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        return R.success(setmealList);
    }

}
